﻿--[[
   @brief    话题直播房间 model
   @author   rickonzhang
   @version  1.0
   @date     2016-08-08
--]]

live_room_model = {
    current_live_room_info_ = nil;  -- 当前话题房间信息
    join_live_room_rsp_ = nil; -- 加入话题房间返回信息
    group_id_to_dlg_id_ = {};
    dlg_id_to_shadow_id_ = {};
    ui_update_inter_ = 60*1; --default: 1 minitue
    is_click_join_btn_ = false;
    is_live_room_closed_ = false;

    unread_live_msg_list_count = {}; --直播小编消息
}

function live_room_model.log(log_str, log_level)
    local is_true = false;
    if log_level == true then
        is_true = true;
    end
    base_utility.log("[live_room] - model - "..log_str, is_true);
end
----------------------------------------------------------------------------------------
function live_room_model.increase_unread_live_msg(group_id, count)
    if live_room_model.unread_live_msg_list_count[group_id] == nil then
        live_room_model.unread_live_msg_list_count[group_id] = 0;
    end
    live_room_model.unread_live_msg_list_count[group_id] = live_room_model.unread_live_msg_list_count[group_id]  + count;
end

function live_room_model.get_unread_live_msg(group_id)
    if live_room_model.unread_live_msg_list_count[group_id] ~= nil then
        return live_room_model.unread_live_msg_list_count[group_id];
    else
        return 0;
    end
end

function live_room_model.clear_unread_live_msg(group_id)
    live_room_model.unread_live_msg_list_count[group_id] = 0;
end

function live_room_model.clear_all_unread_live_msg()
    for key, value in pairs(live_room_model.unread_live_msg_list_count) do
        live_room_model.unread_live_msg_list_count[key] = 0;
    end
end
----------------------------------------------------------------------------------------
function live_room_model.set_ui_update_inter(inter)
    if type(inter) == "number" then
        if inter > 0 then
            live_room_model.ui_update_inter_ = inter;
        end
    end
end

function live_room_model.get_ui_update_inter()
    return live_room_model.ui_update_inter_;
end

function live_room_model.set_cur_live_room_info(room_info)
    live_room_model.current_live_room_info_ = room_info;

    local match_group_info = group_list_model.get_match_room_info(room_info.room_base_info.room_id);
    if match_group_info ~= nil then
        local room_info = {room_info = live_room_model.current_live_room_info_}
        room_info.group_id = match_group_info.group_id;

        comm_center.send_broadcast('i_broadcast_social_live_room_info_changed', room_info);
    end
end

function live_room_model.get_cur_live_room_info()
    return live_room_model.current_live_room_info_;
end

function live_room_model.update_cur_live_room_number(num)
    if live_room_model.current_live_room_info_ ~= nil and 
        live_room_model.current_live_room_info_.cur_match_info ~= nil then
        live_room_model.current_live_room_info_.cur_match_info.member_cnt = num;
        local match_group_info = group_list_model.get_match_room_info(live_room_model.current_live_room_info_.room_base_info.room_id);
        if match_group_info ~= nil then
            local room_info = {room_info = live_room_model.current_live_room_info_}
            room_info.group_id = match_group_info.group_id;

            comm_center.send_broadcast('i_broadcast_social_live_room_info_changed', room_info);
        end
    end
end

function live_room_model.insert_group_id_and_dlg_id_pair(room_id, dlg_id)
    live_room_model.log("insert_group_id_and_dlg_id_pair:"..tostring(room_id)..", "..tostring(dlg_id), true);
    live_room_model.group_id_to_dlg_id_[room_id] = dlg_id;
end

function live_room_model.insert_group_id_and_shadow_id_pair(dlg_id, shadow_id)
    live_room_model.dlg_id_to_shadow_id_[dlg_id] = shadow_id;
end

function live_room_model.remove_dlg_id(dlg_id)
    live_room_model.log("remove_dlg_id:"..tostring(dlg_id), true);
    for key, id in pairs(live_room_model.group_id_to_dlg_id_) do
        if id == dlg_id then
            live_room_model.group_id_to_dlg_id_[key] = nil;
            live_room_model.log("remove_dlg_id success", true);
            break;
        end
    end
end

function live_room_model.remove_shadow_id(shadow_id)
    for key, id in pairs(live_room_model.dlg_id_to_shadow_id_) do
        if id == shadow_id then
            live_room_model.dlg_id_to_shadow_id_[key] = nil;
            break;
        end
    end
end 

function live_room_model.get_dlg_id_from_group_id(group_id)
    live_room_model.log("get_dlg_id_from_group_id:"..tostring(live_room_model.group_id_to_dlg_id_[group_id]), true);
    return live_room_model.group_id_to_dlg_id_[group_id];
end

function live_room_model.get_shadow_id_from_dlg_id(dlg_id)
    return live_room_model.dlg_id_to_shadow_id_[dlg_id];
end

function live_room_model.get_group_id_from_dlg_id(dlg_id)
    live_room_model.log("get_group_id_from_dlg_id:"..tostring(dlg_id), true);
    local room_id = nil;
    for key, id in pairs(live_room_model.group_id_to_dlg_id_) do
        if id == dlg_id then
            room_id = key;
            break;
        end
    end
    live_room_model.log("get_group_id_from_dlg_id return:"..tostring(room_id), true);
    return room_id;
end

function live_room_model.init()
    live_room_model.log("init");
end

function live_room_model.get_web_ctrl_id_by_group_id(group_id)
    local dlg_id = live_room_model.get_dlg_id_from_group_id(group_id);
    if  dlg_id == nil then
        return 0;
    end

    local web_ctrl_id = ui_utility.get_ctrl_id_by_path(dlg_id, "QBWebBrowser_Talk");
    if web_ctrl_id == 0 then
        return 0;
    else
        return web_ctrl_id;
    end
    return 0;
end

function live_room_model.is_room_id_new_flag_showed()
    -- 历史记录中是否已经加入过
    local out = comm_center.call_service("im_app_service_read_common_cfg", {is_self=1, key="social_new_live_room_ids"});
    local id_list = base_utility.json_to_table(out.value);
    local room_id = 0;
    if live_room_model.current_live_room_info_ ~= nil and live_room_model.current_live_room_info_ ~= nil then
        room_id = live_room_model.current_live_room_info_.room_base_info.room_id;
    end

    if room_id > 0 then
        for k, one_item in pairs(id_list) do
            if one_item.room_id == room_id then
                return true;
            end
        end
    end
    
    return live_room_model.is_me_in_match_live_room_group(room_id);
end

function live_room_model.is_me_in_match_live_room_group(room_id)
    local group_info = group_list_model.get_match_room_info(room_id);
    if group_info ~= nil then
        return true;
    end
    return false;
end

function live_room_model.save_room_id_new_flag_showed(room_id)
    local out = comm_center.call_service("im_app_service_read_common_cfg", {is_self=1, key="social_new_live_room_ids"});
    local id_list = base_utility.json_to_table(out.value);
    for key, value in pairs(id_list) do
        if value.room_id == room_id then
            live_room_model.log("id is exist:"..tostring(room_id));
            return;
        end
    end
    local one_item = {};
    one_item.room_id = room_id;
    one_item.time = os.time();
    table.insert(id_list, one_item);
    comm_center.call_service("im_app_service_write_common_cfg", {is_self=1, key="social_new_live_room_ids", value=base_utility.table_to_json(id_list)});
end

live_room_model.init();
